<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分类" prop="categoryIds">
        <el-cascader style="width: 100%" v-model="queryParams.categoryIds" :options="categoryOptions" :props="props" collapse-tags clearable
          ><template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template></el-cascader
        >
      </el-form-item>
      <el-form-item label="信件编号" prop="letterNumber">
        <el-input v-model="queryParams.letterNumber" placeholder="请输入信件编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="属地" prop="deptId" class="deptIdClass">
        <div>
          <treeselect
            v-model="queryParams.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择属地"
          />
        </div>
      </el-form-item> -->
      <el-form-item label="属地" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择属地" clearable>
          <el-option v-for="dict in dict.type.biz_area_owner" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="登记机构" prop="signDept">
        <el-input v-model="queryParams.signDept" placeholder="请输入登记机构" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="涉及机构" prop="referDept">
        <el-input v-model="queryParams.referDept" placeholder="请输入涉及机构" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="姓名" prop="letterUsername">
        <el-input v-model="queryParams.letterUsername" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="letterPhone">
        <el-input v-model="queryParams.letterPhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否初访" prop="isVisit">
        <el-select v-model="queryParams.isVisit" placeholder="是否初访" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否化解" prop="isSolve">
        <el-select v-model="queryParams.isSolve" placeholder="是否化解" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否办结" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="是否办结" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有理" prop="isReasonable">
        <el-select v-model="queryParams.isReasonable" placeholder="是否有理" clearable>
          <el-option v-for="dict in dict.type.biz_reasonable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否涉法涉诉" prop="isLaw">
        <el-select v-model="queryParams.isLaw" placeholder="是否涉法涉诉" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="信访目的" prop="letterPurpose">
        <el-select v-model="queryParams.letterPurpose" placeholder="信访目的" clearable>
          <el-option v-for="dict in dict.type.biz_letter_purpose" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- v-if="display === 'register'||"-->
      <el-form-item label="信件状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="信件状态" clearable>
          <el-option v-for="dict in dict.type.biz_letter_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="分类状态" prop="isCategory">
        <el-select v-model="queryParams.isCategory" placeholder="分类状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_whether_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="台账月份" prop="billMonth">
        <el-date-picker v-model="queryParams.billMonth" type="month" placeholder="选择月" value-format="yyyy-MM"> </el-date-picker>
      </el-form-item>
      <el-form-item label="登记时间">
        <el-date-picker v-model="letterTime" :clearable="true" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @input="$forceUpdate()"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['city:letter:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['city:letter:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['city:letter:remove']"
          >删除</el-button
        >
      </el-col> -->
      <div v-if="display === 'register'">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['city:letter:import']">登记件导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-download" size="mini" @click="downloadTemplate('letter_register_import')">下载导入模板</el-button>
        </el-col>
      </div>
      <div v-if="display === 'claim'">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleClaimImport" v-hasPermi="['city:letter:claim:import']">认领件导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport('CLAIM')" v-hasPermi="['city:letter:register:export']">导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-download" size="mini" @click="downloadTemplate('letter_claim_import')">下载导入模板</el-button>
        </el-col>
      </div>
      <div v-if="display === 'dispatch'">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleDispatchImport" v-hasPermi="['city:letter:dispatch:import']">调度件导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport('DISPATCH')" v-hasPermi="['city:letter:claim:export']">导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-download" size="mini" @click="downloadTemplate('letter_dispatch_import')">下载导入模板</el-button>
        </el-col>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="letterList" stripe :max-height="tableHeight" highlight-current-row highlight-selection-row @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="一级分类" align="center" prop="firstCategoryName" />
      <el-table-column label="二级分类" align="center" prop="secondCategoryName" />
      <el-table-column label="台账月份" align="center" prop="billMonth" />
      <el-table-column label="信件编号" align="center" prop="letterNumber" width="100" />
      <el-table-column label="类别" align="center" prop="caseType" />
      <el-table-column label="信件标题" align="center" prop="letterTitle" :show-overflow-tooltip="true" />
      <!-- <el-table-column
        label="属地"
        align="center"
        prop="deptName"
        width="130"
      /> -->

      <el-table-column label="属地" align="center" prop="deptId" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_area_owner" :value="scope.row.deptId" />
        </template>
      </el-table-column>
      <el-table-column label="问题属地" align="center" prop="ownerArea" width="150" />
      <el-table-column label="登记机构" align="center" prop="signDept" width="150" />
      <el-table-column label="登记时间" align="center" prop="letterTime" width="150" sortable />
      <!-- <el-table-column
        label="问题属地"
        align="center"
        prop="ownerArea"
        width="150"
      /> -->
      <el-table-column label="涉及机构" align="center" prop="referDept" width="150" />
      <el-table-column label="姓名" align="center" prop="letterUsername" />
      <el-table-column label="手机号" align="center" prop="letterPhone" width="110" />
      <el-table-column label="是否初访" align="center" prop="isVisit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isVisit" />
        </template>
      </el-table-column>
      <el-table-column label="重访次数" align="center" prop="repeatVisit" />
      <el-table-column label="信访目的" align="center" prop="letterPurpose" width="130">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_purpose" :value="scope.row.letterPurpose" />
        </template>
      </el-table-column>
      <el-table-column label="内容分类" align="center" prop="letterCategory" :show-overflow-tooltip="true" />
      <el-table-column label="投诉内容" align="center" prop="letterContent" :show-overflow-tooltip="true" />

      <el-table-column label="认领状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- 
      <el-table-column
        label="概况信息"
        align="center"
        prop="letterInfo"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column label="是否有理" align="center" prop="isReasonable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_reasonable" :value="scope.row.isReasonable" />
        </template>
      </el-table-column>
      <el-table-column label="是否涉法涉诉" align="center" prop="isLaw" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isLaw" />
        </template>
      </el-table-column>
      <el-table-column label="是否化解" align="center" prop="isSolve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isSolve" />
        </template>
      </el-table-column>
      <el-table-column label="是否办结" align="center" prop="isFinish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isFinish" />
        </template>
      </el-table-column>
      <el-table-column label="是否重复认领" align="center" prop="isRepeatClaim" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isRepeatClaim" />
        </template>
      </el-table-column>
      <el-table-column label="认领人" align="center" prop="claimBy" />
      <el-table-column label="认领时间" align="center" prop="claimTime" width="150" />
      <el-table-column label="调度人" align="center" prop="dispatchBy" />
      <el-table-column label="调度时间" align="center" prop="dispatchTime" width="150" />
      <el-table-column label="导入时间" align="center" prop="createTime" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row, 1)" v-hasPermi="['city:letter:query']">查看</el-button>
          <div v-if="display === 'claim'">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 2)" v-hasPermi="['city:letter:claim']">认领</el-button>
          </div>

          <div v-if="display === 'dispatch'">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 2)" v-hasPermi="['city:letter:dispatch']">调度</el-button>
          </div>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['city:letter:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['city:letter:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改地市信访对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="categoryIds">
              <el-cascader style="width: 100%" v-model="form.categoryIds" :options="categoryOptions" :props="{ expandTrigger: 'hover' }" clearable></el-cascader>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="信件编号" prop="letterNumber">
              {{ form.letterNumber }}
              <!-- <el-input
                v-model="form.letterNumber"
                placeholder="请输入信件编号"
              /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="信件标题" prop="letterTitle">
              <el-input v-model="form.letterTitle" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登记机构" prop="signDept"> <el-input v-model="form.signDept" placeholder="请输入登记机构" /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="登记时间" prop="letterTime">
              <el-date-picker style="width: 100%" clearable v-model="form.letterTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择登记时间"> </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="letterUsername">
              <el-input v-model="form.letterUsername" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="letterPhone">
              <el-input v-model="form.letterPhone" placeholder="请输入手机号" maxlength="11" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否初访" prop="isVisit">
              <el-select v-model="form.isVisit" placeholder="是否初访" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="重访次数"
              prop="repeatVisit"
              :rules="[
                {
                  pattern: /^\d+?$/,
                  message: '请输入正确参数',
                  trigger: 'change',
                },
              ]"
            >
              <el-input v-model="form.repeatVisit" placeholder="请输入重访次数" maxlength="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="caseType"> <el-input v-model="form.caseType" placeholder="请输入类别" /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="信访目的" prop="letterPurpose">
              <el-select v-model="form.letterPurpose" placeholder="信访目的" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_letter_purpose" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题属地" prop="ownerArea">
              <el-input v-model="form.ownerArea" placeholder="请输入问题属地" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="涉及机构" prop="referDept">
              <el-input v-model="form.referDept" placeholder="请输入涉及机构" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="内容分类" prop="letterCategory">
          <el-input v-model="form.letterCategory" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="概况信息" prop="letterInfo">
          <editor v-model="form.letterInfo" :min-height="150" />
        </el-form-item>
        <el-form-item label="投诉内容">
          <editor v-model="form.letterContent" :min-height="150" />
        </el-form-item>
        <el-form-item label="备注" prop="letterRemark">
          <el-input v-model="form.letterRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-row>
          <!-- <div v-if="display === 'claim' ||view === 1"> -->
          <el-col :span="12">
            <el-form-item label="是否有理" prop="isReasonable">
              <el-radio-group v-model="form.isReasonable" :disabled="display === 'dispatch'">
                <el-radio-button v-for="dict in dict.type.biz_reasonable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否涉法涉诉" prop="isLaw">
              <el-radio-group v-model="form.isLaw">
                <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
              <el-form-item label="是否办结" prop="isFinish">
                <el-radio-group
                  v-model="form.isFinish"
                  :disabled="display === 'claim'"
                >
                  <el-radio-button
                    v-for="dict in dict.type.biz_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio-button
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否化解" prop="isSolve">
                <el-radio-group
                  v-model="form.isSolve"
                  :disabled="display === 'claim'"
                >
                  <el-radio-button
                    v-for="dict in dict.type.biz_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio-button
                  >
                </el-radio-group>
              </el-form-item>
            </el-col> -->
          <!-- </div> -->
          <div v-if="display === 'dispatch' || view === 1">
            <!-- <el-col :span="8">
              <el-form-item label="是否有理" prop="isReasonable">
                <el-radio-group
                  v-model="form.isReasonable"
                  :disabled="display === 'dispatch'"
                >
                  <el-radio-button
                    v-for="dict in dict.type.biz_reasonable"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio-button
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>-->
            <el-col :span="12">
              <el-form-item label="是否办结" prop="isFinish">
                <el-radio-group v-model="form.isFinish">
                  <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否化解" prop="isSolve">
                <el-radio-group v-model="form.isSolve">
                  <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </div>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" v-if="display === 'claim' && view == 2" @click="submitBack">退 回</el-button>
        <el-button type="primary" v-if="view == 2" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport + '&billMonth=' + upload.billMonth + '&importType=' + upload.importType" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :on-error="handleFileError" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm" :loading="upload.submitBtn">
          <b v-if="!upload.submitBtn">确 定</b>
          <b v-else>数据处理中...</b>
        </el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLetter, getLetter, delLetter, addLetter, updateLetter, checkLetter, backLetter } from "@/api/hades/city/letter";
import { deptTreeSelect } from "@/api/system/dept";
import { treeselect } from "@/api/hades/category/category";
import { getToken } from "@/utils/auth";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Letter",
  dicts: ["biz_yes_no", "biz_reasonable", "biz_letter_status", "biz_letter_purpose", "biz_whether_category", "biz_area_owner"],
  components: { Treeselect },
  data() {
    return {
      props: { multiple: true },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地市信访表格数据
      letterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        billMonth: "",
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/letter/import/importData",
        uploadMonth: true,
        importType: "",
        submitBtn: false,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pCategoryId: null,
        sCategoryId: null,
        letterNumber: null,
        signDept: null,
        caseType: null,
        deptName: null,
        letterTime: null,
        letterUsername: null,
        letterPhone: null,
        isVisit: null,
        letterPurpose: null,
        billMonth: null,
        categoryIds: [],
        exportType: null,
        statusList: [],
        deptId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryIds: [{ required: true, message: "请选择来访分类", trigger: "change" }],
        isReasonable: [{ required: true, message: "请选择是否有理", trigger: "blur" }],
        isFinish: [{ required: true, message: "请选择是否办结", trigger: "blur" }],
        isSolve: [{ required: true, message: "请选择是否化解", trigger: "blur" }],
        isLaw: [{ required: true, message: "请选择是否涉法涉诉", trigger: "blur" }],
      },
      letterTime: "",
      categoryOptions: [],
      display: "",
      // 部门树选项
      deptOptions: undefined,
      view: 2,
      // deptNameFilters: [],
    };
  },
  // mounted() {
  //   this.calcBillMonth();
  // },
  computed: {
    tableHeight() {
      return window.innerHeight - 250;
    },
  },
  created() {
    this.display = this.$route.query.router;
    this.calcBillMonth();
    this.getList();
    this.getCategoryList();
    // this.getDeptTree();
  },
  methods: {
    filterHandler(value, row, column) {
      const property = column["property"];
      return row[property] === value;
    },
    calcBillMonth() {
      let date = new Date();
      let year = date.getFullYear();
      switch (this.display) {
        case "claim":
          let month = date.getMonth();
          let m = month < 10 ? "0" + month : month;
          this.queryParams.billMonth = `${year}-${m}`;
          break;
        case "dispatch":
          let dmonth = date.getMonth() - 1;
          let dm = dmonth < 10 ? "0" + dmonth : dmonth;
          this.queryParams.billMonth = `${year}-${dm}`;
          break;
        default:
          break;
      }
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询分类列表 */
    getCategoryList() {
      treeselect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    /** 查询地市信访列表 */
    getList() {
      this.loading = true;
      switch (this.display) {
        case "claim":
          this.queryParams.statusList = ["1", "2", "4"];
          break;
        case "dispatch":
          this.queryParams.statusList = ["2", "3"];
          break;
        default:
          break;
      }
      this.queryParams.exportType = this.display.toUpperCase();
      this.addDateRange(this.queryParams, this.letterTime, "LetterTime");
      listLetter(this.queryParams).then((response) => {
        this.letterList = response.rows;
        this.total = response.total;
        this.loading = false;

        // const uniqueNames = [...new Set(response.rows.map((item) => item.referDept))];
        // this.deptNameFilters = uniqueNames.map((name) => ({ text: name, value: name }));
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pCategoryId: null,
        sCategoryId: null,
        letterNumber: null,
        caseType: null,
        letterTitle: null,
        signDept: null,
        letterTime: null,
        letterUsername: null,
        letterPhone: null,
        repeatVisit: null,
        isVisit: null,
        letterPurpose: null,
        letterCategory: null,
        ownerArea: null,
        referDept: null,
        letterContent: null,
        letterInfo: null,
        letterRemark: null,
        deptId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isReasonable: null,
        isSolve: null,
        isFinish: null,
        categoryIds: [],
        billMonth: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.letterTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地市信访";
    },
    /** 修改按钮操作 */
    handleUpdate(row, view) {
      this.view = view;
      this.reset();
      const id = row.id || this.ids;
      if (view === 1) {
        this.getLetterInfo(id);
        return;
      }
      switch (this.display) {
        case "claim":
          this.check(id);
          break;
        case "dispatch":
          this.getLetterInfo(id);
          break;
        default:
          break;
      }
    },
    check(id) {
      checkLetter(id).then((response) => {
        if (!response.data) {
          this.$confirm("信访件已被他人认领，请确认是否认领？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.getLetterInfo(id);
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消",
              });
            });
        } else {
          this.getLetterInfo(id);
        }
      });
    },
    getLetterInfo(id) {
      getLetter(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "地市信访登记";
      });
    },
    submitBack() {
      this.$confirm("是否退回信访件，退回操作将重置分类及是否有理、是否涉法涉诉，请确认？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          backLetter(this.form.id).then((response) => {
            this.open = false;
            this.getList();
            this.$modal.msgSuccess("操作成功");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    /** 提交按钮 */
    submitForm() {
      // if (this.form.categoryIds[0] === "0") {
      //   this.$message.error("请选择分类");
      //   return;
      // }
      this.form.exportType = this.display.toUpperCase();
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLetter(this.form).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLetter(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除地市信访编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLetter(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(val) {
      if (this.queryParams.billMonth === "" || this.queryParams.billMonth === null) {
        this.$message.error("请选择台账月份");
        return;
      }
      this.queryParams.exportType = val;
      this.addDateRange(this.queryParams, this.letterTime, "LetterTime");
      this.download(
        "letter/export/exportData",
        {
          ...this.queryParams,
        },
        `letter_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
      this.upload.uploadMonth = true;
      this.upload.importType = "REGISTER";
      this.upload.submitBtn = false;
    },
    /** 导入按钮操作 */
    handleClaimImport() {
      this.upload.title = "导入";
      this.upload.open = true;
      this.upload.uploadMonth = true;
      this.upload.importType = "CLAIM";
      this.upload.submitBtn = false;
    },
    handleDispatchImport() {
      this.upload.title = "导入";
      this.upload.open = true;
      this.upload.uploadMonth = true;
      this.upload.importType = "DISPATCH";
      this.upload.submitBtn = false;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 50vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    handleFileError(err, file, fileList) {
      console.log("eee", err);
    },
    // 提交上传文件
    submitFileForm() {
      // console.log("submit", this.upload.billMonth);
      // if (this.upload.billMonth === "" && this.upload.uploadMonth) {
      //   this.$message.error("请选择台账月份");
      //   return;
      // }
      this.upload.submitBtn = true;
      this.$refs.upload.submit();
    },
    downloadTemplate(command) {
      let a = document.createElement("a");
      a.href = `/static/` + command + `.xlsx`;
      a.download = command + ".xlsx";
      a.click();
    },
  },
};
</script>
